package com.example.piece.ui.fragment

import android.graphics.Color
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.example.base.broadcast.Event
import com.example.base.broadcast.EventBusUtil
import com.example.base.ext.dip2px
import com.example.base.ext.getViewModel
import com.example.base.ui.fragment.BaseFragment
import com.example.base.ui.view.DividerItemDecoration
import com.example.base.ui.view.recyclerview.SpaceItemDecoration
import com.example.base.vm.onError
import com.example.piece.R
import com.example.piece.constant.Key
import com.example.piece.databinding.FragmentDianZanBinding
import com.example.piece.entity.UserAttentionListEntity
import com.example.piece.ui.adapter.DianZanAdapter
import com.example.piece.ui.adapter.FansAdapter
import com.example.piece.vm.JokesViewModel


/**
 *
 */
class DianZanFragment : BaseFragment<FragmentDianZanBinding>() {


    companion object {
        @JvmStatic
        fun newInstance(jokeId: String) =
            DianZanFragment().apply {
                arguments = Bundle().apply {
                    putString("jokeId", jokeId)
                }
            }
    }

    private val adapter: DianZanAdapter by lazy {
        val adapter = DianZanAdapter()
        mBinding.rv?.adapter = adapter
        adapter
    }
    private val jokesViewModel: JokesViewModel by lazy {
        getViewModel<JokesViewModel>()
    }

    lateinit var jokeId: String
    var page: Int = 1
    override fun getContentRes(): Int = R.layout.fragment_dian_zan

    override fun initData(view: View, savedInstanceState: Bundle?) {
        super.initData(view, savedInstanceState)
        jokeId = arguments?.getString("jokeId", "").toString()

        mBinding.rv.adapter = adapter
        context?.let {
            mBinding.rv.addItemDecoration(DividerItemDecoration(context,
                DividerItemDecoration.HORIZONTAL_LIST,
                it.dip2px(2f).toInt(),
                Color.parseColor("#F9F9F9")))
        }
        val loadMoreModule = adapter.loadMoreModule
        loadMoreModule.setOnLoadMoreListener {
            getData()
        }
        adapter.setEmptyView(R.layout.layout_empty)
        adapter.setOnItemClickListener(object : OnItemClickListener {
            override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {

            }

        })
        mBinding?.srl?.setOnRefreshListener {
            page = 1
            loadMoreModule.isEnableLoadMore = true
            loadMoreModule.isAutoLoadMore = true
            getData()
        }
        getData()
    }

    private fun getData() {
        jokesViewModel.jokesLikeList(this, jokeId, page.toString()) {
            mBinding?.srl?.isRefreshing = false
            adapter.loadMoreModule.loadMoreComplete()

            if (it.code == 200) {
                page++
                if (it.data.isNullOrEmpty()) {
                    adapter.loadMoreModule.isEnableLoadMore = false
                    adapter.loadMoreModule.isAutoLoadMore = false
                    return@jokesLikeList
                }
                if (page == 2) {
                    adapter.setList(it.data)

                } else {
                    adapter.addData(it.data)

                }

            }
        } onError {

        }
    }

}